package com.sinaapp.mjfinal.mvc.service;

import java.util.ArrayList;
import java.util.List;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.LogAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.sinaapp.mjfinal.mvc.base.BaseService;
import com.sinaapp.mjfinal.mvc.model.Population;

/**
 * 人口业务
 * @author binchao
 * 2016年2月27日
 */
public class PopulationService extends BaseService {
	
	public static final PopulationService service = new PopulationService();
	
	/**
	 * 查询所有人口统计信息
	 * @return
	 */
	public List<Population> findAll(){
		String sql = getSql("mjfinal.population.findAll");
		return Population.dao.find(sql);
	}
	
	/**
	 * 人口统计信息echarts option
	 * @return
	 */
	public String getPopulationForEchartsOption() {
		
		//获取人口统计数据
		List<Population> populations = findAll();
		//设置echarts option
		GsonOption option = new GsonOption();
		
		option.title("全国人口统计数据", "来自国家数据");
        option.tooltip().trigger(Trigger.axis);
        option.legend("总人口", "男性人口", "女性人口", "城镇人口", "乡村人口");
        option.toolbox().show(true).feature(Tool.mark,
                Tool.dataView,
                new MagicType(Magic.line, Magic.bar, Magic.stack, Magic.tiled),
                Tool.restore,
                Tool.saveAsImage).padding(20);
        //年份作为横轴
		List<String> xAxis = new ArrayList<>();
		//总人口
		List<Integer> totalPop = new ArrayList<>();
		//男性人口
		List<Integer> manPop = new ArrayList<>();
		//女性人口
		List<Integer> womenPop = new ArrayList<>();
		//城镇人口
		List<Integer> townPop = new ArrayList<>();
		//乡村人口
		List<Integer> ruralPop = new ArrayList<>();
		for(Population p : populations) {
			xAxis.add(p.getStr("year"));
			totalPop.add(p.getInt("total"));
			manPop.add(p.getInt("man"));
			womenPop.add(p.getInt("women"));
			townPop.add(p.getInt("townpop"));
			ruralPop.add(p.getInt("ruralpop"));
			
		}
        option.calculable(true);
        option.xAxis(new CategoryAxis().boundaryGap(false).data(xAxis.toArray()));
        option.yAxis(new ValueAxis());
        
		Line total = new Line("总人口");
		//total.smooth(true).itemStyle().normal().areaStyle().typeDefault();
		total.data(totalPop.toArray());
		
		Line man = new Line("男性人口");
		//man.smooth(true).itemStyle().normal().areaStyle().typeDefault();
		man.data(manPop.toArray());
		
		Line women = new Line("女性人口");
		//women.smooth(true).itemStyle().normal().areaStyle().typeDefault();
		women.data(womenPop.toArray());
		
		Line town = new Line("城镇人口");
		//town.smooth(true).itemStyle().normal().areaStyle().typeDefault();
		town.data(townPop.toArray());
		
		Line rural = new Line("乡村人口");
		//rural.smooth(true).itemStyle().normal().areaStyle().typeDefault();
		rural.data(ruralPop.toArray());
		
		option.series(total, man, women, town, rural);
        
		return option.toString();
	}
}
